package com.hn.project.rqinspect.controller;

import com.hn.common.exception.BusinessException;
import com.hn.common.utils.SecurityUtils;
import com.hn.doc.xyj.annotation.Api;
import com.hn.framework.web.controller.BaseController;
import com.hn.framework.web.domain.AjaxResult;
import com.hn.framework.web.page.TableDataInfo;
import com.hn.project.rqinspect.domain.dto.*;
import com.hn.project.rqinspect.domain.vo.TaskCheckChangeRecordVo;
import com.hn.project.rqinspect.domain.vo.TaskRecordVo;
import com.hn.project.rqinspect.domain.vo.TaskVo;
import com.hn.project.rqinspect.service.TaskCheckService;
import com.hn.project.rqinspect.service.TaskService;
import com.hn.project.rqinspect.service.UserBranchService;
import com.hn.project.rqinspect.service.UserGroupService;
import com.hn.utils.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("系统管理员")
@RequestMapping("admin")
@RestController
public class AdminController extends BaseController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private UserGroupService userGroupService;

    @Autowired
    private UserBranchService userBranchService;

    @Autowired
    private TaskCheckService taskCheckService;

    /**
     *  总任务统计
     */
    @Api(value = "总任务统计", result = CountTaskNumDto.class)
    @PostMapping("countTask")
    public AjaxResult countTask() {
        Integer userId = SecurityUtils.getUserId();
        return AjaxResult.success(taskService.countTask(userId));
    }

    /**
     * 总任务列表
     */
    @Api(value = "总任务列表", result = TaskDto.class)
    @GetMapping("listTask")
    public TableDataInfo listTask( TaskVo taskVo) {
        Integer userId = SecurityUtils.getUserId();
        // 获取当前用户管理的网点
        List<Integer> branchIds = userBranchService.listBranchIdsByUserId(userId);
        if (branchIds.isEmpty()) {
            return getDataTable(new ArrayList<>());
        }

        // 获取网点下的任务
        List<Integer> taskIds = userGroupService.listTaskIdsByBranchIds(branchIds);
        if (taskIds == null || taskIds.isEmpty()) {
            return getDataTable(new ArrayList<>());
        }

        taskVo.setTaskIds(taskIds);
        startPage();
        return getDataTable(taskService.listTask(taskVo));
    }

    /**
     *  子任务统计
     */
    @Api(value = "子任务统计", result = CountTaskDto.class)
    @GetMapping("countTaskRecord")
    public AjaxResult countTaskRecord(Integer taskId) {
        AssertUtils.notNull(taskId,"请选择任务");
        Integer userId = SecurityUtils.getUserId();
        return AjaxResult.success(taskService.countTaskRecord(userId,taskId));
    }

    /**
     *  任务记录列表
     */
    @Api(value = "任务记录列表", result = TaskRecordDto.class)
    @GetMapping("listTaskRecord")
    public TableDataInfo listTaskRecord(TaskRecordVo taskRecordVo) {
        Integer taskId = taskRecordVo.getTaskId();
        AssertUtils.notNull(taskId,"请先选择任务");

         // 查询管理员管理的网点
        Integer userId = SecurityUtils.getUserId();
        // 获取当前用户管理的网点
        List<Integer> branchIds = userBranchService.listBranchIdsByUserId(userId);
        if (branchIds.isEmpty()) {
            return getDataTable(new ArrayList<>());
        }

        // 获取网点下的任务
        List<Integer> taskIds = userGroupService.listTaskIdsByBranchIds(branchIds);
        if (taskIds == null || taskIds.isEmpty()) {
            return getDataTable(new ArrayList<>());
        }

        if(!taskIds.contains(taskId)){
            throw new BusinessException("暂无权限查询此网点的任务");
        }
        startPage();
        return getDataTable(taskService.listTaskRecord(taskRecordVo));
    }

    /**
     *  审核问题列表
     * @param auditStatus 0=待审核 1=审核通过
     */
    @Api(value = "审核问题列表",result = TaskQuesCheckRecordDto.class)
    @GetMapping("listAuditCheckRecord")
    public TableDataInfo listAuditCheckRecord(Integer auditStatus) {
        AssertUtils.notNull(auditStatus,"auditStatus 必传");
        startPage();
        Integer userId = SecurityUtils.getUserId();
        return getDataTable(taskCheckService.listAuditCheckRecord(userId,auditStatus));
    }

    /**
     * 审核问题详情
     */
    @Api(value = "审核问题详情",result = TaskQuesCheckRecordDetailDto.class)
    @GetMapping("checkRecordDetail")
    public AjaxResult checkRecordDetail(Integer checkRecordId) {
        return AjaxResult.success(taskCheckService.checkRecordDetail(checkRecordId));
    }

    /**
     *  审核整改信息
     */
    @Api(value = "审核整改信息")
    @PostMapping("auditChangeRecord")
    public AjaxResult auditChangeRecord(@RequestBody TaskCheckChangeRecordVo taskCheckChangeRecordVo) {
        return toAjax(taskCheckService.auditChangeRecord(taskCheckChangeRecordVo));
    }

}
